﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using Xceed.Wpf.Toolkit;
using System.IO;
using System.Collections.ObjectModel;

namespace Jiuyong
{
	namespace Views
	{
		/// <summary>
		/// SuperReplacePage.xaml 的交互逻辑
		/// </summary>
		public partial class SuperReplacePage : Page
		{
			public SuperReplacePage()
			{
				InitializeComponent();
				Loaded += Page_Loaded;
			}

			ViewModels.SuperReplaceViewModel ViewModel
			{
				get
				{
					return DataContext as ViewModels.SuperReplaceViewModel;
				}
			}

			void Page_Loaded(object sender, RoutedEventArgs e)
			{
				CommandBindings.AddRange(new CommandBindingCollection()
				{
					new CommandBinding(ApplicationCommands.SaveAs,ViewModel.SelectSavePath)
					,
					new CommandBinding(ApplicationCommands.Open, ViewModel.SelectOpenPath)
					,
					new CommandBinding(ApplicationCommands.New,ViewModel.GetInputString)
					,
					new CommandBinding(ApplicationCommands.Replace,ViewModel.GetOutputString)
					,
					new CommandBinding(MediaCommands.Play,ViewModel.Run)
					,
					new CommandBinding(ApplicationCommands.Save,ViewModel.SaveSetting)
					,
					new CommandBinding(ApplicationCommands.Delete,ViewModel.ClearOutput)
					,
					new CommandBinding(ApplicationCommands.Properties,ViewModel.GetSkipPath)
					,
					new CommandBinding(MediaCommands.Select,ViewModel.SelectConfig)
				});

				//ViewModel.OutputLine = r =>
				//{
				//    OutputLines.Inlines.Add(r);
				//    OutputLines.Inlines.Add(new System.Windows.Documents.LineBreak());
				//};
			}

		}
	}

	namespace ViewModels
	{
		public class SuperReplaceViewModel : FilesViewModel<SuperReplaceConfig>
		{
			//public SuperReplaceViewModel()
			//{
			//	ConfigSetting = Config<SuperReplaceConfig>.Load();
			//	LoadSettings();
			//}

			//private void LoadSettings()
			//{
			//	InputText = ConfigSetting.InputText;
			//	InputPath = ConfigSetting.InputPath;
			//	OutputText = ConfigSetting.OutputText;
			//	OutputPath = ConfigSetting.OutputPath;
			//}

			//protected override void LoadSettings()
			//{
			//    base.LoadSettings();
			//    SkipPath = ConfigSettings.Default.SkipPath;
			//}

			protected override void LoadConfig(SuperReplaceConfig cs)
			{
				base.LoadConfig(cs);
				SkipPath = cs.SkipPath;
			}

			//private void SaveSettings()
			//{
			//	//ConfigSetting.InputText = InputText;
			//	//ConfigSetting.InputPath = InputPath;
			//	//ConfigSetting.OutputText = OutputText;
			//	//ConfigSetting.OutputPath = OutputPath;
			//	base.SaveSettings();
			//	//ConfigSetting.Save();
			//}

			//protected override void SaveSettings()
			//{
			//    ConfigSettings.Default.SkipPath = SkipPath;
			//    base.SaveSettings();
			//}

			public override void SaveConfig(SuperReplaceConfig cs)
			{
				cs.SkipPath = SkipPath;
				base.SaveConfig(cs);
			}

			//private string _inputPath;
			//public string InputPath
			//{
			//	get
			//	{
			//		//return _inputPath??"文件夹1？";
			//		//return String.Format("“文件夹：{0}”",_inputPath??"？");
			//		return _inputPath;
			//	}

			//	set
			//	{
			//		_inputPath = value;
			//		OnPropertyChanged("InputPath");
			//	}
			//}

			//private string _inputText;
			//public string InputText
			//{
			//	get
			//	{
			//		//return String.Format("“字符串：{0}”", _inputText ?? "？");
			//		return _inputText;
			//	}

			//	set
			//	{
			//		_inputText = value;
			//		OnPropertyChanged("InputText");
			//	}
			//}

			//private string _outputPath;
			//public string OutputPath
			//{
			//	get
			//	{
			//		//return String.Format("“文件夹：{0}”", _outputPath ?? "？");
			//		return _outputPath;
			//	}

			//	set
			//	{
			//		_outputPath = value;
			//		OnPropertyChanged("OutputPath");
			//	}
			//}

			//private string _outputText;
			//public string OutputText
			//{
			//	get
			//	{
			//		//return String.Format("“字符串：{0}”", _outputText ?? "？");
			//		return _outputText;
			//	}

			//	set
			//	{
			//		_outputText = value;
			//		OnPropertyChanged("OutputText");
			//	}
			//}

			//private string _saveResult;
			//public string SaveResult
			//{
			//	get
			//	{
			//		return _saveResult;
			//	}

			//	set
			//	{
			//		_saveResult = value;
			//		OnPropertyChanged("SaveResult");
			//	}
			//}

			//private ObservableCollection<string> _outputLines = new ObservableCollection<string>();
			//public ObservableCollection<string> OutputLines
			//{
			//	get
			//	{
			//		return _outputLines;
			//	}

			//	set
			//	{
			//		_outputLines = value;
			//		OnPropertyChanged("OutputLines");
			//	}
			//}

			//private string _fileContent;
			//public string FileContent
			//{
			//	get
			//	{
			//		return _fileContent;
			//	}

			//	set
			//	{
			//		_fileContent = value;
			//		OnPropertyChanged("FileContent");
			//	}
			//}

			//private string _selectFile;
			//public string SelectFile
			//{
			//	get
			//	{
			//		return _selectFile;
			//	}

			//	set
			//	{
			//		_selectFile = value;
			//		OnPropertyChanged("SelectFile");
			//		if (File.Exists(value))
			//		{
			//			FileContent = File.ReadAllText(value);
			//		}
			//		else
			//		{
			//			FileContent = null;
			//		}
			//	}
			//}

			private string _skipPath;
			public string SkipPath
			{
				get
				{
					return _skipPath;
				}

				set
				{
					_skipPath = value;
					OnPropertyChanged("SkipPath");
				}
			}

			//public Action<string> OutputLine;

			//public void GetInputString(object sender, ExecutedRoutedEventArgs e)
			//{
			//	var panel = e.Parameter as Panel;
			//	panel.ShowTextDialog(r =>
			//	{
			//		InputText = r;
			//	},defaultText:InputText);
			//}

			//public void GetOutputString(object sender, ExecutedRoutedEventArgs e)
			//{
			//	var panel = e.Parameter as Panel;

			//	panel.ShowTextDialog(r =>
			//	{
			//		OutputText = r;
			//	}, defaultText: OutputText);
			//}

			public void GetSkipPath(object sender,ExecutedRoutedEventArgs e)
			{
				var panel = e.Parameter as Panel;

				panel.ShowTextDialog(r =>
				{
					SkipPath = r;
				},defaultText: SkipPath);
			}


			public void Run(object sender, ExecutedRoutedEventArgs e)
			{
				var fs = Directory.EnumerateFiles(this.InputPath, "*", SearchOption.AllDirectories);
				var skips = SkipPath.Split(";,，；".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);

				foreach (var f in fs)
				{
					//文件路径替换。
					var f1 = f.Substring(InputPath.Length);
					foreach (var skip in skips)
					{
						if (f1.Contains(skip))
						{
							goto skip;
						}
					}

					var f2 = f1.Replace(InputText, OutputText);
					var f3 = OutputPath + f2;
					//nfs.Add(f3);

					//文件内容替换。f->f3
					var c0 = File.ReadAllText(f);
					var c1 = c0.Replace(InputText, OutputText);
					var dir = System.IO.Path.GetDirectoryName(f3);
					if (Directory.Exists(dir))
					{
					}
					else
					{
						Directory.CreateDirectory(dir);
					}
					File.WriteAllText(f3, c1);
					//OutputLine(f3);
					OutputLines.Add(f3);
			skip://跳过文件的处理;
				;
				}
			}

			//public void SelectSavePath(object sender, ExecutedRoutedEventArgs e)
			//{
			//	ToolBox.Core.SelectPathDialog(r =>
			//	{
			//		OutputPath = r;
			//	}
			//	,
			//	initialDirectory:OutputPath
			//	);
			//}

			//public void SelectOpenPath(object sender, ExecutedRoutedEventArgs e)
			//{
			//	ToolBox.Core.SelectPathDialog(r =>
			//	{
			//		InputPath = r;
			//	}
			//	,
			//	initialDirectory:InputPath
			//	);
			//}

			//public void SaveSetting(object sender, ExecutedRoutedEventArgs e)
			//{
			//	SaveSettings();
			//	SaveResult = String.Format("（已保存设置,时间：{0:dd日HH:mm:ss}）", DateTime.Now);
			//}

			//public void ClearOutput(object sender, ExecutedRoutedEventArgs e)
			//{
			//	OutputLines.Clear();
			//}

		}
	}

	[Serializable]
    public class SuperReplaceConfig:FilesConfig
    {
		public string SkipPath;
		//public override void Save()
		//{
		//    Config<SuperReplaceConfig>.Save(this);
		//}
		//public SuperReplaceConfig() : base()
		//{
		//}

		//public SuperReplaceConfig(SuperReplaceConfig sourceConfig) : base(sourceConfig)
		//{
		//}
    }
}
